import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_class2/3681/news/NewsWebViewPage.dart';
import 'package:flutter_class2/common/global.dart';
import 'package:url_launcher/url_launcher.dart';

import 'news_json_resp.dart';

class ListBuilder{
  static List<Widget> genWidgetsFromJson(NewsData newsData, BuildContext context){
    List<Widget> returnData= new List();
    List<DataItem> dataItems= newsData.result.data.dataItems;
    for(var i=0;i<dataItems.length;i++){
      returnData.add(ListItem.genSingleItem(dataItems[i], context));
    }
    return returnData;
  }
}

// 每一个列表子项（主要是newsCard）
class ListItem{
  static Widget genSingleItem(DataItem dataItem, BuildContext context){
    String  uniquekey=dataItem.uniquekey;
    String  title=dataItem.title;
    String  date=dataItem.date;
    String  category=dataItem.category;
    String  author_name=dataItem.author_name;
    String  url=dataItem.url;
    String  thumbnail_pic_s=dataItem.thumbnail_pic_s;
    String  thumbnail_pic_s02=dataItem.thumbnail_pic_s02;
    String  thumbnail_pic_s03=dataItem.thumbnail_pic_s03;

    return Container(
      padding: EdgeInsets.all(10.0),
      child: InkWell(
        onTap: (){
          print("news_list uniquekey: "+uniquekey);
          Navigator.of(context).push(
              MaterialPageRoute(builder: (context)=>NewsDetailPage(title:title, url: url,))
          );
        },
        child: newsCard(
            thumbnail_pic_s,
            title,
            date,
            author_name,
        ),
      ),
    );
  }

  static void launchUrl(String url) async{
    if(await canLaunch(url)){
      await launch(url);
    }else{
      throw 'Could not launch $url';
    }
  }
}

// 自定义一个新闻的卡片
Widget newsCard(String imagePath, String title, String date, String author_name) {
  final currentColor = Colors.white70;// 统一卡片与边框的颜色

  final newsTitle = Container(
    width: Global.screenWidth - 25*2 - 120 - 20 - 60, // 根据屏幕尺寸调整文本尺寸
    child:Text(
      title,
      softWrap: true,
      maxLines: 2,
      overflow: TextOverflow.ellipsis,//设置字符过长时显示省略号
      style: TextStyle(fontSize: 18,fontWeight: FontWeight.bold),
    ),
  );

  final newsDateAndAuthor = Container(
    width: Global.screenWidth - 25*2 - 120 - 20 - 60,
    child: Text(
      date + "  " + author_name,
      softWrap: true,
      maxLines: 1,
      overflow: TextOverflow.ellipsis,//在写死container宽度的基础上设置字符过长时显示省略号
      style: TextStyle(fontSize: 14,),
    ),
  );

  return Container(
    height: 120,
    alignment: Alignment.center,
    decoration: BoxDecoration(
      color: currentColor,//背景色
      borderRadius: BorderRadius.all(Radius.circular(15.0)),//设置四周圆角 角度
      border: new Border.all(width: 0, color: currentColor),//设置四周边框
    ),
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Padding(
          padding: EdgeInsets.all(10.0),
          child: ClipRRect(
            borderRadius: BorderRadius.all(Radius.circular(15.0)),
            child: Image.network(imagePath),
          ),
        ),
        SizedBox(width: 15,),
        Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween, // 将主轴空白位置进行均分，排列子元素，首尾没有空隙
          crossAxisAlignment: CrossAxisAlignment.start,// 将主轴元素左对齐
          children: [
            Padding(
                padding: EdgeInsets.only(top: 15),
                child: newsTitle
            ),
            Padding(
              padding: EdgeInsets.only(bottom: 15),
              child: newsDateAndAuthor,
            ),
          ],
        ),
      ],
    ),
  );
}